package com.conncui.common.tool;


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by cui on 16-6-15.
 */
public class LifeCycle {
    public final static int CALL_TO_SUPER = 1;
    public final static int RETURN_FROM_SUPER = 2;
    static String TAG = "LifeCycle";

    public static void refLifeCycle(Object object, @LifeCycleState int lifeCycleState) {
        Class objClass = object.getClass();
        String note = "";
        switch (lifeCycleState) {
            case CALL_TO_SUPER:
                note = "<-";
                break;
            case RETURN_FROM_SUPER:
                note = "->";
                break;
            default:
        }
        System.out.println(
                TAG + ","
                        + objClass.getSimpleName() + ","
                        + Integer.toHexString(object.hashCode()) + ","
                        + getMethodName() + ","
                        + note);
    }

    public static String getMethodName() {
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        return elements[4].getMethodName();
    }

    public static String getCurrentMethodName() {
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        return elements[3].getMethodName();
    }

    @IntDef({CALL_TO_SUPER, RETURN_FROM_SUPER})
    @Retention(RetentionPolicy.SOURCE)
    public @interface LifeCycleState {
    }
}
